package com.example.administrator.xxnapp.ui;

import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Paint;
import android.graphics.Path;
import android.support.annotation.Nullable;
import android.util.AttributeSet;
import android.view.View;
import android.view.ViewGroup;
import android.widget.LinearLayout;

import com.example.administrator.xxnapp.R;

import cn.mdruby.baselibrary.utils.PixelUtil;

/**
 * Created by Went_Gone on 2018/5/8.
 */

public class RectangleHalf extends ViewGroup {
    private int viewWidth,viewHeight;
    private Paint mPaint;

    public RectangleHalf(Context context) {
        this(context,null);
    }

    public RectangleHalf(Context context, @Nullable AttributeSet attrs) {
        this(context, attrs,0);
    }

    public RectangleHalf(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);

        mPaint = new Paint();
//        mPaint.setStrokeWidth(1);
        mPaint.setStyle(Paint.Style.STROKE);
        mPaint.setStrokeWidth(PixelUtil.dp2px(2,context));
        mPaint.setColor(getResources().getColor(R.color.base_color));
    }

    @Override
    protected void onLayout(boolean b, int i, int i1, int i2, int i3) {

    }


    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        super.onMeasure(widthMeasureSpec, heightMeasureSpec);
        // 获取宽-测量规则的模式和大小
        int widthMode = MeasureSpec.getMode(widthMeasureSpec);
        int widthSize = MeasureSpec.getSize(widthMeasureSpec);

        // 获取高-测量规则的模式和大小
        int heightMode = MeasureSpec.getMode(heightMeasureSpec);
        int heightSize = MeasureSpec.getSize(heightMeasureSpec);

        // 设置wrap_content的默认宽 / 高值
        // 默认宽/高的设定并无固定依据,根据需要灵活设置
        // 类似TextView,ImageView等针对wrap_content均在onMeasure()对设置默认宽 / 高值有特殊处理,具体读者可以自行查看
        int mWidth = 60;
        int mHeight = 40;

        // 当布局参数设置为wrap_content时，设置默认值
        if (getLayoutParams().width == ViewGroup.LayoutParams.WRAP_CONTENT && getLayoutParams().height == ViewGroup.LayoutParams.WRAP_CONTENT) {

            setMeasuredDimension(mWidth, mHeight);
            viewWidth = mWidth;
            viewHeight = mHeight;
            // 宽 / 高任意一个布局参数为= wrap_content时，都设置默认值
        } else if (getLayoutParams().width == ViewGroup.LayoutParams.WRAP_CONTENT) {
            setMeasuredDimension(mWidth, heightSize);
            viewWidth = mWidth;
            viewHeight = heightSize;
        } else if (getLayoutParams().height == ViewGroup.LayoutParams.WRAP_CONTENT) {
            setMeasuredDimension(widthSize, mHeight);
            viewWidth = widthSize;
            viewHeight = mHeight;
        }else {
            setMeasuredDimension(widthSize, heightSize);
            viewWidth = widthSize;
            viewHeight = heightSize;
        }
    }

    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);

        Path path = new Path();
        path.moveTo(0,0);
//        path.lineTo(viewWidth-20,0);
//        path.quadTo(viewWidth,0,viewWidth,20);
        path.lineTo(viewWidth,viewHeight);
        path.lineTo(0,viewHeight);
        canvas.drawPath(path,mPaint);
    }
}
